package org.frame.security.project.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.frame.common.core.entity.Dept;
import org.frame.common.core.entity.Role;
import org.frame.common.core.entity.User;
import org.frame.security.framework.web.controller.BaseController;
import org.frame.security.project.system.domain.SysDept;
import org.frame.security.project.system.domain.SysRole;
import org.frame.security.project.system.domain.SysUser;
import org.frame.security.project.system.service.ISysMenuService;
import org.frame.security.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户信息 接口
 * @author ty-intasect03
 *
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController
{
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysMenuService menuService;
    
    /**
     * 接口：根据用户名称获取详细信息
     * @param userName 用户名
     * @return
     */
    @GetMapping(value = "/info/{userName}")
    public User userInfo(@PathVariable String userName) {
    	log.info("根据用户名：{}查询用户信息", userName);
    	SysUser u = userService.selectUserByUserName(userName);
    	if(u == null) {
    		return null;
    	}
    	SysDept d = u.getDept();
		Dept dept = null;
		if(d != null) {
			dept = new Dept(d.getDeptId(), d.getParentId(), d.getAncestors(), d.getDeptName(), 
				d.getOrderNum(), d.getLeader(), d.getPhone(), d.getEmail(), d.getStatus(), 
				d.getDelFlag(), d.getParentName(), getDeptChildren(d.getChildren()));
		}
		List<Role> roles = null;
		if(u.getRoles() != null) {
			roles = sysRole2Role(u.getRoles());
		}
		User user = new User(u.getUserId(), u.getDeptId(), u.getUserName(), u.getNickName(), u.getEmail(), u.getPhonenumber(), 
				u.getSex(), u.getAvatar(), u.getPassword(), u.getSalt(), u.getStatus(), u.getDelFlag(), u.getLoginIp(), 
				u.getLoginDate(), dept, roles, u.getRoleIds(), u.getPostIds());
		log.info("根据用户名：{}查询到用户：{}", userName, user);
        return user;
    }
    
    /**
     * 接口：根据用户id获取权限信息
     * @param userId 用户id
     * @return
     */
    @GetMapping(value = "/perms/{userId}")
    public Set<String> userPerms(@PathVariable Long userId) {
    	log.info("根据用户id：{}查询权限", userId);
    	return menuService.selectMenuPermsByUserId(userId);
    }
    
    
    
    private List<Role> sysRole2Role(List<SysRole> srs){
		if(srs == null || srs.size() == 0) {
			return null;
		}
		List<Role> roles = new ArrayList<Role>();
		Role role = null;
		for (SysRole r : srs) {
			role = new Role(r.getRoleId(), r.getRoleName(), r.getRoleKey(), r.getRoleSort(), r.getDataScope(), 
					r.getStatus(), r.getDelFlag(), false, r.getMenuIds(), r.getDeptIds());
			roles.add(role);
		}
		return roles;
	}
	
	private List<Dept> getDeptChildren(List<SysDept> sds) {
		List<Dept> children = new ArrayList<Dept>();
		if(sds != null && sds.size() > 0) {
			Dept dept = null;
			for (SysDept d : sds) {
				dept = new Dept(d.getDeptId(), d.getParentId(), d.getAncestors(), d.getDeptName(), 
						d.getOrderNum(), d.getLeader(), d.getPhone(), d.getEmail(), 
						d.getStatus(), d.getDelFlag(), 
						d.getParentName(), getDeptChildren(d.getChildren()));
				children.add(dept);
			}
		}
		return children;
	}
}